#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Solution {
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
        int m = mat.size();
        int n = mat[0].size();
        vector<vector<int>> as(m, vector<int>(n, -1));
        queue<pair<int, int>> ad;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (mat[i][j] == 0)
                {
                    ad.push({ i,j });
                    as[i][j] = 0;
                }
            }
        }
        int dx[] = { 0,0,1,-1 };
        int dy[] = { 1,-1,0,0 };
        while (ad.size())
        {
            auto [a, b] = ad.front();
            ad.pop();
            for (int i = 0; i < 4; i++)
            {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n && as[x][y] == -1)
                {
                    as[x][y] = as[a][b] + 1;
                    ad.push({ x,y });
                }

            }
        }
        return as;
    }
};